﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Shapes;

namespace HaumDisplay
{
    public class HaumRectangle : HaumShape
    {
        /// <summary>
        /// 左上角坐标
        /// </summary>
        public PointF location;

        /// <summary>
        /// 宽高
        /// </summary>
        public SizeF size;

        internal Pen pen;

        /// <summary>
        /// 反色
        /// </summary>
        internal Color invertColor;

        /// <summary>
        /// 是否选中
        /// </summary>
        internal bool isSelect;

        /// <summary>
        /// 开始绘制
        /// </summary>
        internal bool startGraphic;

        public HaumRectangle(Color color, int borderSize = 1)
        {
            this.location = PointF.Empty;
            this.size = SizeF.Empty;
            this.pen = new Pen(color, borderSize);
            //this.invertColor = Color.FromArgb(255 - color.R, 255 - color.G, 255 - color.B);
            this.invertColor = Color.FromArgb(color.R / 2, color.G / 2, color.B / 2);
            this.isSelect = false;
            this.startGraphic = false;
        }

        /// <summary>
        /// 获取矩形左上点坐标
        /// </summary>
        /// <returns></returns>
        public PointF GetTL()
        {
            return new PointF(this.location.X, this.location.Y);
        }

        /// <summary>
        /// 获取矩形右上点坐标
        /// </summary>
        /// <returns></returns>
        public PointF GetTR()
        {
            return new PointF(this.location.X + this.size.Width, this.location.Y);
        }

        /// <summary>
        /// 获取矩形右下点坐标
        /// </summary>
        /// <returns></returns>
        public PointF GetBR()
        {
            return new PointF(this.location.X + this.size.Width, this.location.Y + this.size.Height);
        }

        /// <summary>
        /// 获取矩形左下点坐标
        /// </summary>
        /// <returns></returns>
        public PointF GetBL()
        {
            return new PointF(this.location.X, this.location.Y + this.size.Height);
        }

        /// <summary>
        /// 获取矩形角点
        /// </summary>
        /// <returns></returns>
        public PointF[] GetContours()
        {
            return new PointF[4] { this.GetTL(), this.GetTR(), this.GetBR(), this.GetBL() };
        }

        /// <summary>
        /// 获取矩形中心点
        /// </summary>
        /// <returns></returns>
        public PointF GetCenter()
        {
            float x = this.location.X + this.size.Width / 2;
            float y = this.location.Y + this.size.Height / 2;
            return new PointF(x, y);
        }

        /// <summary>
        /// 获取矩形面积
        /// </summary>
        /// <returns></returns>
        public float GetArea()
        {
            return this.size.Width * this.size.Height;
        }

        /// <summary>
        /// 获取矩形周长
        /// </summary>
        /// <returns></returns>
        public float GetPermiter()
        {
            return (this.size.Width + this.size.Height) * 2;
        }

        public override string ToString()
        {
            return $"矩形起始坐标:[{location.X},{location.Y}]-宽:[{size.Width.ToString("f3")}]-高:[{size.Height.ToString("f3")}]-" +
                $"角点坐标[" +
                $"TL[{this.GetTL().X},{this.GetTL().Y}]," +
                $"TR[{this.GetTR().X},{this.GetTR().Y}]," +
                $"BR[{this.GetBR().X},{this.GetBR().Y}]," +
                $"BL[{this.GetBL().X},{this.GetBL().Y}]]";
        }

        public override void Graphic(Graphics g, GraphicHelper myGraphic)
        {
            g.SmoothingMode = SmoothingMode.HighQuality;
            //获取圆心像素坐标 转换成控件坐标
            PointF center = Tools.ImageTransScreenPointF(this.GetCenter(), myGraphic.scale, myGraphic.offset);
            //根据中心坐标拟合两条垂直线
            // 绘制垂直的线（中心点上下各 3 像素）
            g.DrawLine(this.pen, center.X, center.Y - 5, center.X, center.Y + 5);
            // 绘制水平的线（中心点左右各 3 像素）
            g.DrawLine(this.pen, center.X - 5, center.Y, center.X + 5, center.Y);

            PointF rectTlLoca = Tools.ImageTransScreenPointF(this.GetTL(), myGraphic.scale, myGraphic.offset);
            PointF rectBrLoca = Tools.ImageTransScreenPointF(this.GetBR(), myGraphic.scale, myGraphic.offset);
            g.DrawRectangle(this.pen, rectTlLoca.X, rectTlLoca.Y, rectBrLoca.X - rectTlLoca.X, rectBrLoca.Y - rectTlLoca.Y);


            int r = (int)this.pen.Width * 3;

            using (Brush brush = new SolidBrush(this.isSelect ? this.invertColor : this.pen.Color))
            {
                //PointF br = Tools.ImageTransScreenPointF(this.GetBR(), myGraphic.scale, myGraphic.offset);
                //g.FillEllipse(brush, br.X - r / 2, br.Y - r / 2, r, r);
            }
        }

    }
}
